namespace CodeProject.Podium.PowerPoint.Service
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    public class RegistrationService : IRegistrationService
    {
        public static event EventHandler<SubscriptionEventArgs> Subscribed;

        private static List<Uri> subscribers = new List<Uri>();
        private static object obj = new object();

        #region IRegistrationService Members
        public void Register(string uri)
        {
            Uri channelUri = new Uri(uri, UriKind.Absolute);
            Subscribe(channelUri);
        }

        public void Unregister(string uri)
        {
            Uri channelUri = new Uri(uri, UriKind.Absolute);
            Unsubscribe(channelUri);
        }
        #endregion

        #region Subscribtion/Unsubscribtion logic
        private void Subscribe(Uri channelUri)
        {
            lock (obj)
            {
                if (!subscribers.Exists((u) => u == channelUri))
                {
                    subscribers.Add(channelUri);
                }
            }
            OnSubscribed(channelUri, true);
        }

        public static void Unsubscribe(Uri channelUri)
        {
            lock (obj)
            {
                subscribers.Remove(channelUri);
            }
            OnSubscribed(channelUri, false);
        }
        #endregion

        #region Helper private functionality
        private static void OnSubscribed(Uri channelUri, bool isActive)
        {
            EventHandler<SubscriptionEventArgs> handler = Subscribed;
            if (handler != null)
            {
                handler(null,
                    new SubscriptionEventArgs(channelUri, isActive));
            }
        }
        #endregion

        #region Internal SubscriptionEventArgs class definition
        public class SubscriptionEventArgs : EventArgs
        {
            public SubscriptionEventArgs(Uri channelUri, bool isActive)
            {
                this.ChannelUri = channelUri;
                this.IsActive = isActive;
            }

            public Uri ChannelUri { get; private set; }
            public bool IsActive { get; private set; }
        }
        #endregion

        #region Helper public functionality
        public static List<Uri> GetSubscribers()
        {
            return subscribers;
        }
        #endregion

        public static SlideShowService Service { get; set; }

        public byte[] GetSlide(Guid guid)
        {
            if (Service != null)
                return Service.GetSlide(guid);

            var value = ASCIIEncoding.Unicode.GetBytes("This is a test message");
            return value;
        }

        public void NextSlide()
        {
            if (Service != null)
                Service.NextSlide();
        }
        public void PreviousSlide()
        {
            if (Service != null)
                Service.PreviousSlide();
        }
    }
}